package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16778
 * @date 2025/10/7 12:56
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result page(@RequestParam(required = false) String name,
                       @RequestParam(required = false) String label,
                       Integer page,Integer pageSize){
        PageResult<Role> res = roleService.page(name,label,page,pageSize);
        return Result.success(res);
    }

    @LogOperation
    @PostMapping
    public Result insert(@RequestBody Role role){
        log.info("待插入的角色：{}",role);
        roleService.insert(role);
        return Result.success();
    }

    @LogOperation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("需要删除的id：{}",id);
        roleService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("需要查找的id：{}",id);
        Role role = roleService.selectById(id);
        return Result.success(role);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("需要更新的数据:{}",role);
        roleService.update(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Role> list = roleService.findAll();
        return Result.success(list);
    }
}
